package com.ccl.iot.object;
public class IOTRegInfo {
	public long iDevID = 0;
	public int iAddress = 0;
	public int iSize = 0;

	public IOTRegInfo(){
	}

	public IOTRegInfo(IOTRegInfo aInfo){
		if (aInfo != null){
			iDevID = aInfo.iDevID;
			iAddress = aInfo.iAddress;
		}
	}
	
	public IOTRegInfo(long aDevID, int aAddress, int aSize){
		iDevID = aDevID;
		iAddress = aAddress;
		iSize = aSize;
	}
	
	public boolean isValid(){
		return iDevID != 0 && iAddress >= 0 && iSize > 0;
	}
	
	public boolean expand(int aAddress, int aSize){
		if (aAddress == iAddress + iSize || iAddress == aAddress + aSize){
			if (aAddress < iAddress){
				iAddress = aAddress;
			}
			
			iSize += aSize;
			
			return true;
		}
		
		return false;
	}
	
	public void join(int aAddress, int aSize){
		int tSize;
		
		if (aAddress < iAddress){
			tSize = iAddress - aAddress + iSize;
			
			if (aSize > tSize){
				iSize = aSize;
			}else{
				iSize = tSize;
			}
			
			iAddress = aAddress;
		}else{
			tSize = aAddress - iAddress + aSize;
			
			if (iSize < tSize){
				iSize = tSize;
			}
		}
	}
	
	public IOTRegInfo clone() {
		return new IOTRegInfo(this);
	}

	@Override
	public String toString() {
		return iDevID + "-" + iAddress + "-" + iSize;
	}
}
